﻿using System;
using System.Collections;
using UnityEngine;

namespace ZSW.Framework.Utils
{
    public class ZSWFUtil_Game
    {
        /// <summary>
        /// 延迟执行
        /// </summary>
        /// <param name="seconds"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static IEnumerator DelayedExecution(float seconds, Action action, Action<float> progress = null)
        {
            float currentTime = seconds;
            while (true)
            {
                currentTime -= UnityEngine.Time.deltaTime;
                if (currentTime <= 0f)
                {
                    break;
                }

                progress?.Invoke(1f - currentTime / seconds);
                yield return new WaitForSeconds(0.016f);
            }

            action?.Invoke();
        }

        /// <summary>
        /// 插值到目标位置
        /// </summary>
        /// <param name="target"></param>
        /// <param name="targetPose"></param>
        /// <param name="callback"></param>
        /// <param name="speed"></param>
        /// <returns></returns>
        public static IEnumerator LerpMoveTo(Transform target, Transform targetPose, Action callback = null, float speed = 2f)
        {
            while (true)
            {
                target.position = Vector3.Lerp(target.position, targetPose.position, speed * UnityEngine.Time.deltaTime);
                target.rotation = Quaternion.Lerp(target.rotation, targetPose.rotation, speed * UnityEngine.Time.deltaTime);
                if (Vector3.Distance(target.position, targetPose.position) < 0.1f && Quaternion.Angle(target.rotation, targetPose.rotation) < 0.1f)
                {
                    break;
                }

                yield return null;
            }

            target.position = targetPose.position;
            target.rotation = targetPose.rotation;
            callback?.Invoke();
        }
    }
}